package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberDao memberDao;

    //查会员
    @Override
    public Member findMemberByTel(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    /**
     * 注册会员
     *
     * @param member
     */
    @Override
    public void add(Member member) {
        memberDao.add(member);
    }

    @Override
    public Integer findMemberCountBeforeDate(String month) {
        return memberDao.findMemberCountBeforeDate(month);
    }

    @Override
    public Map<String, Object> getSexReport() {
        /**
         *  man :通过sql语句查询 性别为1的 总人数
         *
         * 女:通过sql语句查询 性别为0的 总人数
         *
         * {
         flag:true,
         message:'成功',
         data:{
         setmealNames:['男','女'],
         setmealCount:[
         {value:1, name:'男'},
         {value:2, name:'女'},
         ]
         }
         }
         */

        //查询多条所需记录
        List<Map> mapList = memberDao.getSexReport();
        //封装转化所需记录
        List<String> sexNames = new ArrayList<>();
        if (mapList != null && mapList.size() > 0) {
            for (Map map : mapList) {
                String name = (String) map.get("name");
                /*if (name.equals("0")) {
                    name = "女";
                } else {
                    name = "男";
                }*/
                /*if (name=="0") {
                    name="女";
                }else {
                    name="男";
                }*/
                sexNames.add(name);
            }
        }
        //返回前端需要的记录
        Map<String, Object> map = new HashMap<>();
        map.put("sexNames", sexNames);
        map.put("sexCount", mapList);
        return map;


    }
    /**
     * 年龄段占比统计
     */
    @Override
    public Map<String, Object> getAgeReport() {
        //查询多条所需记录
        List<Map> ageCount = memberDao.getAgeReport();
        //封装转化所需记录
        List<String> ageNames = new ArrayList<>();
        if (ageCount != null && ageCount.size() > 0) {
            for (Map map : ageCount) {
                String name = (String) map.get("name");
                /*if (name.equals("0")) {
                    name = "女";
                } else {
                    name = "男";
                }*/
                /*if (name=="0") {
                    name="女";
                }else {
                    name="男";
                }*/
                ageNames.add(name);
            }
        }
        //返回前端需要的记录
        Map<String, Object> map = new HashMap<>();
        map.put("ageNames", ageNames);
        map.put("ageCount", ageCount);
        return map;

    }
}
